//
//  DLMachineModel.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/27.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLMachineModel.h"

@implementation DLMachineModel

+ (DLMachineModel *)configModel:(NSDictionary *)dataDic {
    
    NSString *code = [NSString stringWithFormat:@"%@", dataDic[@"code"]];
    NSString *msg = [NSString stringWithFormat:@"%@", dataDic[@"msg"]];
    
    DLMachineModel *machineModel = [DLMachineModel new];
    machineModel.msgFrom = SocketMsgFrom_other;
    machineModel.code = code;
    machineModel.msg = msg;
    
//    NSLog(@"code是：%@", machineModel.code);
    
    if ([machineModel.code isEqualToString:@"1"]) {
        //正常返回值
        NSLog(@"code==1，正常返回值");
        machineModel.resultDic = dataDic[@"result"];
        
        machineModel.type = [NSString stringWithFormat:@"%@", dataDic[@"result"][@"type"]];
        NSLog(@"type是：%@", machineModel.type);
        machineModel.isServerMsgCanTransformToVoice = NO;
        if (machineModel.type.integerValue == 1 || machineModel.type.integerValue == 2) {
            //短文本 长文本
            NSLog(@"长短文本");
            machineModel.msgType = SocketMsgType_text;
            machineModel.content = [NSString stringWithFormat:@"%@", dataDic[@"result"][@"data"]];
            machineModel.isServerMsgCanTransformToVoice = YES;
        } else if (machineModel.type.integerValue == 3) {
            //单选数据
            NSLog(@"code==1,单选数据");
            machineModel.msgType = SocketMsgType_singleSelect;
//            这个是resultArr
        } else if (machineModel.type.integerValue == 4) {
            //多选数据
            NSLog(@"多选数据");
        } else if (machineModel.type.integerValue == 5) {
            //图像
            NSLog(@"图像");
            machineModel.msgType = SocketMsgType_image;
            machineModel.resultDic = dataDic[@"result"];
        } else if (machineModel.type.integerValue == 6) {
            //视频
            NSLog(@"视频");
            machineModel.msgType = SocketMsgType_video;
            machineModel.resultDic = dataDic[@"result"];
        } else if (machineModel.type.integerValue == 7) {
            //表格
            NSLog(@"表格");
            machineModel.msgType = SocketMsgType_form;
            machineModel.resultDic = dataDic[@"result"];
            
        } else if (machineModel.type.integerValue == 8) {
            //柱状图
            NSLog(@"柱状图");
            machineModel.msgType = SocketMsgType_histogram;
            machineModel.resultDic = dataDic[@"result"];
        } else if (machineModel.type.integerValue == 9) {
            //饼图
            NSLog(@"饼图");
            machineModel.msgType = SocketMsgType_pieChart;
            machineModel.resultDic = dataDic[@"result"];
        } else if (machineModel.type.integerValue == 10) {
            //k线图
            NSLog(@"k线图");
            machineModel.msgType = SocketMsgType_kLineChart;
            machineModel.resultDic = dataDic[@"result"];
        } else if (machineModel.type.integerValue == 11) {
            //新闻资讯列表
            NSLog(@"资讯列表");
            machineModel.msgType = SocketMsgType_information;
            machineModel.resultDic = dataDic[@"result"];
        } else if (machineModel.type.integerValue == 12) {
            //分时图
            NSLog(@"分时图");
            machineModel.msgType = SocketMsgType_timeSharing;
            machineModel.resultDic =  dataDic[@"result"];;
        } else if (machineModel.type.integerValue == 99) {
            //预设命令
            NSLog(@"预设命令");
            machineModel.msgType = SocketMsgType_cmd;
        } else if (machineModel.type.integerValue == 66) {
            //默认列表
            NSLog(@"默认列表");
            machineModel.msgType = SocketMsgType_defaultTable;
        } else {
            NSString *errString = @"type不是默认类型中的哟~!";
            kShowRequestMsg(errString);
        }
        
    } else if ([machineModel.code isEqualToString:@"2"]) {
        //意图识别
        NSLog(@"code==2，意图识别，单选数据");
        machineModel.msgType = SocketMsgType_singleSelect;
        machineModel.resultArr = dataDic[@"result"];
    } else if ([machineModel.code isEqualToString:@"6"]) {
        //关联推荐
        NSLog(@"code==6，关联推荐");
        machineModel.resultArr = dataDic[@"result"];
    } else if ([machineModel.code isEqualToString:@"9"]) {
        //异常
        NSLog(@"code==9，异常");
        machineModel.msgType = SocketMsgType_serverError;
        
        machineModel.resultArr = @[];
        machineModel.resultDic = @{};
        [[UIApplication sharedApplication].keyWindow makeToast:(kSafeString(dataDic[@"msg"])) duration:5 position:CSToastPositionCenter];
        
    } else {
        
        NSString *errString = [NSString stringWithFormat:@"异常之外的异常,code是：%@", machineModel.code];
        NSLog(@"%@", errString);
        kShowRequestMsg(errString);
    }
    
    return machineModel;
}

- (NSString *) configKlineDataWithModel:(DLMachineModel *)model {
    
    NSString *str = @"";
//    NSMutableArray *mutArr = [NSMutableArray array];
    
    NSDictionary *dic = model.resultDic[@"data"];
    NSArray *kDataArray = [NSArray arrayWithArray:dic[@"kData"]];
    
    NSDictionary *lastDic = [kDataArray lastObject];
//    NSLog(@"=========k线最后一个数据是：%@", lastDic);
    
//    NSDate *date = [NSDate date];
//    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
//    dateFormatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";
//    NSString *dateStr = [dateFormatter stringFromDate:date];
//    NSString *klineLastDateStr = lastDic[@"date"];
    
//    1.已有今日K线数据，文案：当前点位xx，较前一交易日上涨/下跌3%，上涨/下跌xx点。
//    2.无今日K线数据，文案：当前点位xx
    
    model.isKLineMsg = YES;
    
    NSDictionary *lastLastDic = [kDataArray objectAtIndex:kDataArray.count - 2];
//    NSLog(@"=========k线倒数第二个数据是：%@", lastLastDic);
    
    NSString *lastColse = [NSString stringWithFormat:@"%@", lastDic[@"close"]];
    
    NSString *lastLastColse = [NSString stringWithFormat:@"%@", lastLastDic[@"close"]];
    
    NSString *currentClose = [NSString stringWithFormat:@"%@", lastDic[@"close"]];
    model.currentClose = currentClose;
    
    CGFloat upOrDownPercent = (lastColse.doubleValue - lastLastColse.doubleValue) / lastLastColse.doubleValue * 100;
    model.upOrDownPercent = [NSString stringWithFormat:@"%.2f", upOrDownPercent];
    
    CGFloat upOrDownNum = lastColse.doubleValue - lastLastColse.doubleValue;
    model.upOrDownNum = [NSString stringWithFormat:@"%.2f", upOrDownNum];
    
    model.isGoUp = upOrDownPercent >= 0? YES: NO;
    
    NSString *currentCloseString = [NSString stringWithFormat:@"当前点位:%@", currentClose];
    
    NSString *upOrDownPercentString = upOrDownPercent >= 0?[NSString stringWithFormat:@"较前一交易日上涨%.2f", fabs(upOrDownPercent)]:[NSString stringWithFormat:@"较前一交易日下跌%.2f", fabs(upOrDownPercent)];
    
    NSString *upOrDownNumString = upOrDownPercent >= 0?[NSString stringWithFormat:@"上涨%.2f点", fabs(upOrDownNum)]:[NSString stringWithFormat:@"下跌%.2f点", fabs(upOrDownNum)];
    
    str = [NSString stringWithFormat:@"%@,%@%%,%@。", currentCloseString, upOrDownPercentString, upOrDownNumString];
    
    return str;
}

@end
